app.controller("noteAddCtrl",function($scope,$http,$ionicLoading){
    //用户数据
    var userInfo = JSON.parse(localStorage.getItem("UserInfo"));
    var noteData = JSON.parse(localStorage.getItem("noteData"));
    // var getPath = JSON.parse(localStorage.getItem("getPath"));
    $scope.note = {
        noteTitle:noteData.noteCardName,
        noteContent:""
    };
    angular.element(document).ready(function(){
        //查询笔记
        $scope.getContent();
    });
    //查询
    $scope.getContent = function () {
        var url = getPath.url+getPath.name+"/app/findReadNote";
        var param = {
            userId:userInfo.userId,
            noteCardId:noteData.noteCardId
        }
        $http({
            url: url,
            method: "POST",
            headers:{'Content-Type':'application/x-www-form-urlencoded'},
            data: param
        }).success(function (data) {
                if (data.resultCode == 200) {
                    var res = data.resultBean;
                    if(res){
                        $scope.note = {
                            noteTitle:res.noteTitle,
                            noteContent:res.noteContent
                        };
                        noteData.noteNum = res.noteNum;
                    }else{
                        noteData.noteNum = "";
                    }
                } else {
                    $ionicLoading.show({
                        template: "获取笔记失败！",
                        duration: 3000
                    });
                }
            })
            .error(function () {
                $ionicLoading.hide();
                $ionicLoading.show({
                    template: "获取阅读章节失败",
                    duration: 3000
                });
            });
    }
    //保存
    $scope.saveNote = function () {
        var url = getPath.url+getPath.name+"/app/saveNote";
        $scope.note = angular.copy($scope.note);
        var noteTitle = $scope.note.noteTitle;
        var noteContent = $scope.note.noteContent;
        if(!noteTitle){
            $ionicLoading.show({
                template: "标题不能为空！",
                duration:3000
            });
            $state.go('home.today');
        }
        if(!noteContent){
            $ionicLoading.show({
                template: "内容不能为空！",
                duration:3000
            });
            return;
        }
        var param = {
            userId:userInfo.userId,
            noteIndex:1,
            noteTitle:noteTitle,
            noteContent:noteContent,
            noteBookId:noteData.noteBookId,
            noteChapterId:noteData.noteChapterId,
            noteCardId:noteData.noteCardId,
            noteNum:noteData.noteNum
        }
        $http({
            url: url,
            method: "POST",
            data:param,
            headers:{'Content-Type':'application/x-www-form-urlencoded'}
        }).success(function(data){
                var res = data.resultBean;
                if(data.resultCode==200){
                    $ionicLoading.show({
                        template: "录入笔记成功！",
                        duration:3000
                    });
                }else{
                    $ionicLoading.show({
                        template: "录入笔记失败！",
                        duration:3000
                    });
                }
            })
            .error(function(){
                $ionicLoading.show({
                    template: "录入笔记失败！",
                    duration:3000
                });
            });
    }
});